package com.wyclabs.cardmaker;
import java.io.File;
import java.nio.charset.StandardCharsets;

import cn.hutool.core.io.FileUtil;
import com.wyclabs.cardmaker.constants.CardLevel;
import com.wyclabs.cardmaker.constants.CardType;

/**
 * @author TylerW
 * @date 06/23/21 10:42 AM
 */
public class CardGenerator {

    public static void createCard(Card card, File folder) {
        if (CardType.CREATURE.getType().equals(card.getType().getValue())) {
            createCreature(card, folder);
        } else if (CardType.MAGIC.getType().equals(card.getType().getValue())) {
            createMagic(card, folder);
        } else if (CardType.HERO.getType().equals(card.getType().getValue())) {
            createHero(card, folder);
        }
    }

    private static void createCreature(Card card, File folder) {
        File tmpFile = CommonService.readResource("resource/creature.gd");
        String cards = FileUtil.readString(tmpFile, StandardCharsets.UTF_8);
        cards = cards.replace("#parentClass#", CommonService.config.getProperty("base_card"));
        cards = commonReplace(cards, card);
        File fcard = new File(folder.getPath() + File.separator + card.getPath().getValue() + ".gd");
        FileUtil.writeString(cards, fcard, StandardCharsets.UTF_8);
    }

    private static void createMagic(Card card, File folder) {
        File tmpFile = CommonService.readResource("resource/magic.gd");
        String cards = FileUtil.readString(tmpFile, StandardCharsets.UTF_8);
        cards = cards.replace("#parentClass#", CommonService.config.getProperty("base_card"));
        cards = commonReplace(cards, card);
        File fcard = new File(folder.getPath() + File.separator + card.getPath().getValue() + ".gd");
        FileUtil.writeString(cards, fcard, StandardCharsets.UTF_8);
    }

    private static void createHero(Card card, File folder) {
        String tskName = card.getPath().getValue() + "_Tsk";
        File tmpFile = CommonService.readResource("resource/hero.gd");
        String cards = FileUtil.readString(tmpFile, StandardCharsets.UTF_8);
        cards = cards.replace("#parentClass#", CommonService.config.getProperty("base_hero"));
        cards = commonReplace(cards, card);
        cards = cards.replace("#tskId#", tskName);
        File fcard = new File(folder.getPath() + File.separator + card.getPath().getValue() + ".gd");
        FileUtil.writeString(cards, fcard, StandardCharsets.UTF_8);

        File tmpTskFile = CommonService.readResource("resource/tsk.gd");
        String tskStr = FileUtil.readString(tmpTskFile, StandardCharsets.UTF_8);
        tskStr = commonReplace(tskStr, card);

        File tskFolder = FileUtil.mkdir(CommonService.selectedFolder + File.separator + tskName);
        File ftsk = new File(tskFolder.getPath() + File.separator + card.getPath().getValue() + "_Tsk.gd");
        FileUtil.writeString(tskStr, ftsk, StandardCharsets.UTF_8);
    }

    private static String commonReplace(String cardStr, Card card) {
        cardStr = cardStr.replace("#hp#", card.getHp().getValue());
        cardStr = cardStr.replace("#cry#", card.getCost().getValue());
        cardStr = cardStr.replace("#lv#", CardLevel.getCodeByType(card.getLv().getValue()));
        cardStr = cardStr.replace("#atk#", card.getAtk().getValue());
        cardStr = cardStr.replace("#camp#", CommonService.selectedCamp);
        cardStr = cardStr.replace("#name#", card.getName().getValue());
        cardStr = cardStr.replace("#tag#", card.getTag().getValue());
        cardStr = cardStr.replace("#desc#", card.getDesc().getValue());
        return cardStr;
    }

}
